package com.polaris.live.adapter.backpack

import android.graphics.Paint
import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.polaris.live.R
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.databinding.ItemCommodity2Binding
import com.polaris.live.databinding.ItemCommodityBinding
import com.polaris.live.databinding.ItemCommodityPartyBinding
import com.polaris.live.resp.back_resp.PayerShopResp
import com.polaris.live.utils.NinePatchDrawableUtil
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.transformRounded
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * CommodityAdapter
 *
 * @author Created by 半仙 on 2024/1/31/031 11:44
 */
class CommodityAdapter : BaseQuickAdapter<PayerShopResp, BaseCommodityHolder<*, PayerShopResp>>(0) {

    private var mSelectIndex = -1

    override fun getDefItemViewType(position: Int): Int {
        return data[position].objectType.toInt()
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): BaseCommodityHolder<*, PayerShopResp> {
        return when (viewType) {
            CommonConst.DressUp.USER_PROFILE.toInt(), CommonConst.DressUp.LIVE_EFFECT.toInt() -> {
                Commodity2Holder(
                    ItemCommodity2Binding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            CommonConst.DressUp.PARTY_BACKGROUND.toInt() -> {
                CommodityPartyHolder(
                    ItemCommodityPartyBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            else -> {
                CommodityHolder(
                    ItemCommodityBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

        }

    }

    override fun convert(holder: BaseCommodityHolder<*, PayerShopResp>, item: PayerShopResp) {
        holder.setSelect(mSelectIndex)
        holder.bind(item)
    }

    override fun convert(
        holder: BaseCommodityHolder<*, PayerShopResp>, item: PayerShopResp, data: List<Any>,
    ) {
        when (data.getOrNull(0)) {
            "show" -> holder.show()
            "hide" -> holder.hide()
        }
    }

    fun setSelect(position: Int) {
        notifyItemChanged(mSelectIndex)

        mSelectIndex = position

        notifyItemChanged(mSelectIndex)
    }

    fun getLastSelect() = mSelectIndex

    class CommodityHolder(binding: ItemCommodityBinding) :
        BaseCommodityHolder<ItemCommodityBinding, PayerShopResp>(binding) {

        private var mSelectIndex = -1
        override fun setSelect(position: Int) {
            mSelectIndex = position
        }

        override fun show() {
            mBinding.tvName.startAndResume()
        }

        override fun hide() {
            mBinding.tvName.pause()
        }

        override fun bind(item: PayerShopResp) {
            mBinding.apply {
                discountTv.setBackgroundResource(0)
                val position = absoluteAdapterPosition
                if (position == mSelectIndex) {
                    consTitle.setBackgroundResource(R.drawable.back_pack_select_back)
                } else {
                    consTitle.setBackgroundResource(R.drawable.r8_back_ffffff)
                }
                tvName.setText(item.shopName, true)
                Glide.with(icImage.context)
                    .load(ImageUtils.imgPath(item.displayUrl))
                    .into(icImage)
            }
            if (item.cornerMarkUrl == null) {
                mBinding.discountTv.visibility = View.GONE
            } else {
                mBinding.discountTv.visibility = View.VISIBLE
                mBinding.discountTv.text = item.cornerMarkText
                NinePatchDrawableUtil.getNinePatchDrawable(item.cornerMarkUrl,
                    Rect(7.autoDp, 0.autoDp, 7.autoDp, 0.autoDp), { nine ->
                        mBinding.discountTv.background = nine
                    }) {

                }
            }
            item.coins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        mBinding.diamondLl.visibility = View.VISIBLE
                        val discountCon = item.discountCoins?.get(firstData.key)
                        if (discountCon != null) {
                            mBinding.diamondTv.text = "$discountCon"
                            mBinding.diamondYTv.visibility = View.VISIBLE
                            mBinding.diamondYTv.text = "$con"
                            mBinding.diamondYTv.paintFlags =
                                mBinding.diamondYTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            mBinding.diamondTv.text = "$con"
                            mBinding.diamondYTv.visibility = View.GONE
                        }
                    }
                }
            }
            item.gameCoins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        mBinding.gameCoinLl.visibility = View.VISIBLE
                        val discountCon = item.discountGameCoins?.get(firstData.key)
                        if (discountCon != null) {
                            mBinding.gameCoinTv.text = "$discountCon"
                            mBinding.gameCoinCostTv.visibility = View.VISIBLE
                            mBinding.gameCoinCostTv.text = "$con"
                            mBinding.gameCoinCostTv.paintFlags =
                                mBinding.gameCoinCostTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            mBinding.gameCoinTv.text = "$con"
                            mBinding.gameCoinCostTv.visibility = View.GONE
                        }
                    }
                }
            }
        }

    }

    class CommodityPartyHolder(binding: ItemCommodityPartyBinding) :
        BaseCommodityHolder<ItemCommodityPartyBinding, PayerShopResp>(binding) {

        private var mSelectIndex = -1
        override fun setSelect(position: Int) {
            mSelectIndex = position
        }

        override fun show() {
            mBinding.tvName.startAndResume()
        }

        override fun hide() {
            mBinding.tvName.pause()
        }

        override fun bind(item: PayerShopResp) {
            mBinding.apply {
                val position = absoluteAdapterPosition
                if (position == mSelectIndex) {
                    consTitle.setBackgroundResource(R.drawable.back_pack_select_back)
                } else {
                    consTitle.setBackgroundResource(R.drawable.r8_back_ffffff)
                }

                tvName.setText(item.shopName, true)
                Glide.with(icImage.context)
                    .load(ImageUtils.imgPath(item.displayUrl))
                    .transformRounded(4.autoDp)
                    .into(icImage)
            }

            if (item.cornerMarkUrl == null) {
                mBinding.discountTv.visibility = View.GONE
            } else {
                mBinding.discountTv.visibility = View.VISIBLE
                mBinding.discountTv.text = item.cornerMarkText
                NinePatchDrawableUtil.getNinePatchDrawable(item.cornerMarkUrl,
                    Rect(7.autoDp, 0.autoDp, 7.autoDp, 0.autoDp), { nine ->
                        mBinding.discountTv.background = nine
                    }) {

                }
            }
            item.coins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        mBinding.diamondLl.visibility = View.VISIBLE
                        val discountCon = item.discountCoins?.get(firstData.key)
                        if (discountCon != null) {
                            mBinding.diamondTv.text = "$discountCon"
                            mBinding.diamondYTv.visibility = View.VISIBLE
                            mBinding.diamondYTv.text = "$con"
                            mBinding.diamondYTv.paintFlags =
                                mBinding.diamondYTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            mBinding.diamondTv.text = "$con"
                            mBinding.diamondYTv.visibility = View.GONE
                        }
                    }
                }
            }
            item.gameCoins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        mBinding.gameCoinLl.visibility = View.VISIBLE
                        val discountCon = item.discountGameCoins?.get(firstData.key)
                        if (discountCon != null) {
                            mBinding.gameCoinTv.text = "$discountCon"
                            mBinding.gameCoinCostTv.visibility = View.VISIBLE
                            mBinding.gameCoinCostTv.text = "$con"
                            mBinding.gameCoinCostTv.paintFlags =
                                mBinding.gameCoinCostTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            mBinding.gameCoinTv.text = "$con"
                            mBinding.gameCoinCostTv.visibility = View.GONE
                        }
                    }
                }
            }
        }

    }

    class Commodity2Holder(binding: ItemCommodity2Binding) :
        BaseCommodityHolder<ItemCommodity2Binding, PayerShopResp>(binding) {

        private var mSelectIndex = -1

        override fun setSelect(position: Int) {
            mSelectIndex = position
        }

        override fun show() {
            mBinding.tvName.startAndResume()
        }

        override fun hide() {
            mBinding.tvName.pause()
        }

        override fun bind(item: PayerShopResp) {
            mBinding.apply {
                val position = absoluteAdapterPosition
                if (position == mSelectIndex) {
                    consTitle.setBackgroundResource(R.drawable.effect_back_pack_select_back)
                } else {
                    consTitle.setBackgroundResource(R.drawable.r8_back_ffffff)
                }

                tvName.setText(item.shopName, true)
                Glide.with(icImage.context)
                    .load(ImageUtils.imgPath(item.displayUrl))
                    .into(icImage)
                if (item.objectType.toInt() == CommonConst.DressUp.LIVE_EFFECT.toInt() && AppCodeUtils.isAR()) {
                    icImage.scaleX = -1f
                }
            }

            if (item.cornerMarkUrl == null) {
                mBinding.discountTv.visibility = View.GONE
            } else {
                mBinding.discountTv.visibility = View.VISIBLE
                mBinding.discountTv.text = item.cornerMarkText
                NinePatchDrawableUtil.getNinePatchDrawable(item.cornerMarkUrl,
                    Rect(7.autoDp, 0.autoDp, 7.autoDp, 0.autoDp), { nine ->
                        mBinding.discountTv.background = nine
                    }) {

                }
            }
            item.coins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        mBinding.diamondLl.visibility = View.VISIBLE
                        val discountCon = item.discountCoins?.get(firstData.key)
                        if (discountCon != null) {
                            mBinding.diamondTv.text = "$discountCon"
                            mBinding.diamondYTv.visibility = View.VISIBLE
                            mBinding.diamondYTv.text = "$con"
                            mBinding.diamondYTv.paintFlags =
                                mBinding.diamondYTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            mBinding.diamondTv.text = "$con"
                            mBinding.diamondYTv.visibility = View.GONE
                        }
                    }
                }
            }
            item.gameCoins?.let {
                if (it.isNotEmpty()) {
                    val firstData = it.entries.firstOrNull()
                    firstData?.value?.let { con ->
                        mBinding.gameCoinLl.visibility = View.VISIBLE
                        val discountCon = item.discountGameCoins?.get(firstData.key)
                        if (discountCon != null) {
                            mBinding.gameCoinTv.text = "$discountCon"
                            mBinding.gameCoinCostTv.visibility = View.VISIBLE
                            mBinding.gameCoinCostTv.text = "$con"
                            mBinding.gameCoinCostTv.paintFlags =
                                mBinding.gameCoinCostTv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                        } else {
                            mBinding.gameCoinTv.text = "$con"
                            mBinding.gameCoinCostTv.visibility = View.GONE
                        }
                    }
                }
            }
        }

    }
}

abstract class BaseCommodityHolder<VB : ViewBinding, T>(binding: VB) :
    BaseBindingViewHolder<VB, T>(binding) {

    open fun setSelect(position: Int) {

    }

    open fun show() {

    }

    open fun hide() {

    }
}